import { Config } from '../cors/config';
import { BaseCommand } from '../types/common.interface';
import { RenderTrigger } from '../cors/render-trigger';
import { Select } from '../cors/select';
import { classTransformToObj, objTransformToClass } from '../decorators/toJSON';

export class HiddenColCommand extends BaseCommand {
    protected pastHiddenCols: number[] = [];
    protected pastSelect: any = {};
    constructor(
        protected conf: Config,
        protected render: RenderTrigger,
        protected select: Select,
        protected cols: number[],
        protected type: 'add'|'remove'
        
    ) {
        super();
    }
    execute(): boolean {
        this.pastHiddenCols = Array.from(this.conf.hiddenCols);
        this.pastSelect = classTransformToObj(this.select);
        this.select.selected = null;
        for(let col of this.cols) {
            if(this.type === 'add') {
                this.conf.hiddenCols.add(col);
            } else {
                this.conf.hiddenCols.delete(col);
            }
        }
        this.render.triggerRenderAll();
        return true;
    }
    redo(): boolean {
        this.conf.hiddenCols.clear();
        for(let col of this.pastHiddenCols) {
            this.conf.hiddenCols.add(col);
        }
        objTransformToClass(this.select, this.pastSelect);
        this.render.triggerRenderAll();
        return true;
    }
} 